package com.gjr.demo;

import com.gjr.eduservice.EduApplication;
import com.gjr.entity.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SpringBootTest(classes = EduApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class StreamDemo {
    private static Stream<Student> studentStream;

    @Test
    public void test() {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰");

        //1.拿到所有姓张的 2.拿到名字长度为3个字的 3.打印这些数据
        list.stream()
                .filter(s -> s.startsWith("张"))
                .filter(s -> s.length() == 3)
                .forEach(str -> System.out.println(str));

        Optional<Integer> min = Stream.of(1, 3, 4, 3, 5).min(((o1, o2) -> o1 - o2));
        System.out.println("最小值：" + min.get());
    }

    {
        studentStream = Stream.of(
                new Student("赵丽颖", 52, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 55),
                new Student("柳岩", 52, 33));
    }

    /**
     * 流中数据进行多级分组
     */
    @Test
    public void test1() {
        // 需求：先根据年龄分组，每组中再根据成绩分组
        // Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream
        Map<Integer, Map<String, List<Student>>> groupByStudent = studentStream.collect(Collectors.groupingBy(Student::getAge,
                Collectors.groupingBy(student -> student.getSocre() > 60 ? "及格" : "不及格")
        ));

        groupByStudent.forEach((k, v) -> {
            System.out.println(k);
            v.forEach((key, value) -> {
                System.out.println("\t" + key + "==" + value);
            });
        });
    }

    /**
     * 对流中的数据进行分区
     * Collectors.partitioningBy：会根据值是否为 true，将集合分割为两个列表，一个true 列表，一个false列表
     */
    @Test
    public void test2() {
        Map<Boolean, List<Student>> collect = studentStream.collect(Collectors.partitioningBy(student -> student.getSocre() > 60));

        collect.forEach((key, value) -> {
            System.out.println(key + "==" + value);
        });
    }

    /**
     * 对流中的数据进行拼接
     * Collectors.joining：会根据指定的连接符，将所有元素连成一个字符串
     */
    @Test
    public void test3() {
        // 根据一个字符串拼接
        //String names = studentStream.map(Student::getName).collect(Collectors.joining("--"));

        // 根据三个字符串拼接
        String names = studentStream.map(Student::getName).collect(Collectors.joining("-", "~_~", "v_v"));
        System.out.println(names);
    }


    /**
     * 将流中的数据收集到集合中
     */
    @Test
    public void test4() {
        Stream<String> stream = Stream.of("aa", "bb", "cc");
        //stream.collect(Collectors.toList());
        //stream.collect(Collectors.toSet());

        // 收集到指定的集合类型中
        ArrayList<String> list = stream.collect(Collectors.toCollection(ArrayList::new));
    }

    /**
     * 聚合计算 Collectors.maxBy、minBy、counting、summingInt、averagingInt
     */
}
